package testview.zhen.com.myapplication;

import android.Manifest;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lzf.easyfloat.EasyFloat;
import com.lzf.easyfloat.anim.DefaultAnimator;
import com.lzf.easyfloat.interfaces.OnFloatCallbacks;

import testview.zhen.com.myapplication.mvp.MainContract;
import testview.zhen.com.myapplication.mvp.MainPersenter;
import testview.zhen.com.myapplication.mvp.base.BaseMVPActivity;
import testview.zhen.com.myapplication.tool.AudioRecorder;
import testview.zhen.com.myapplication.tool.FileHelper;
import testview.zhen.com.myapplication.view.RecordButton;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;


public class RecordActivity extends BaseMVPActivity implements MainContract.View {

    private static final String TAG = "RecordActivity";
    private RecordButton recordButton;//录制的按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button button = findViewById(R.id.btn_click);
        recordButton = (RecordButton) findViewById(R.id.phone_view_page_voice_recording);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO}, 1);
        }else {
            Toast.makeText(this,"您已经申请了权限!",Toast.LENGTH_SHORT).show();
            recordButton.setAudioRecorder(new AudioRecorder());
            recordButton.getAudioRecorder().setFileFolder(FileHelper.recordDIR);
            recordButton.setRecorderListener(new RecordButton.RecorderListener() {
                @Override
                public void onRecordStart(AudioRecorder audioRecorder) {
                    Log.i(TAG, "onRecordEnd: audioRecoder:"+audioRecorder.getFilePath());
                }

                @Override
                public void onRecordEnd(AudioRecorder audioRecorder) {
                    Log.i(TAG, "onRecordEnd: audioRecoder:"+audioRecorder.getFilePath());
                }

                @Override
                public void onRecordCanceled(AudioRecorder audioRecorder) {

                }

                @Override
                public void onRecordDisenable(AudioRecorder audioRecorder) {

                }
            });
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//               MainPersenter s =  (MainPersenter)mPresenter;
//               s.login("mingzi","jksdf");
            }
        });




    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1){
            for (int i = 0;i<permissions.length;i++){
                if (grantResults[i] == PERMISSION_GRANTED){
                    recordButton.setAudioRecorder(new AudioRecorder());
                    recordButton.getAudioRecorder().setFileFolder(FileHelper.recordDIR);
                    recordButton.setRecorderListener(new RecordButton.RecorderListener() {
                        @Override
                        public void onRecordStart(AudioRecorder audioRecorder) {
                            Log.i(TAG, "onRecordEnd: audioRecoder:"+audioRecorder.getFilePath());
                        }

                        @Override
                        public void onRecordEnd(AudioRecorder audioRecorder) {
                            Log.i(TAG, "onRecordEnd: audioRecoder:"+audioRecorder.getFilePath());
                        }

                        @Override
                        public void onRecordCanceled(AudioRecorder audioRecorder) {

                        }

                        @Override
                        public void onRecordDisenable(AudioRecorder audioRecorder) {

                        }
                    });
                }
            }
        }
    }

    //    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        Log.e("TAG","RecordActivity  :  onTouchEvent  " + ParseEventStr.parseEventStr(event.getAction()));
//        return super.onTouchEvent(event);
//    }
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        Log.e("TAG","RecordActivity  :  dispatchTouchEvent  " + ParseEventStr.parseEventStr(ev.getAction()));
//        return super.dispatchTouchEvent(ev);
//    }


    @Override
    public void initPresenterAndView() {
        mPresenter = new MainPersenter();
        mPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showLoading() {
        Log.i(TAG, "showLoading: ");
    }

    @Override
    public void hideLoading() {
        Log.i(TAG, "hideLoading: ");
    }

    @Override
    public void onError(Throwable throwable) {
        Log.i(TAG, "onError: ");
    }

    public void showDialogs(){
        EasyFloat.with(this).setLayout(R.layout.activity_main)
//                .setShowPattern(ShowPattern.ALL_TIME)
                //.setShowPattern(ShowPattern.ALL_TIME).setSidePattern(SidePattern.RESULT_HORIZONTAL)
         //设置浮窗的标签，用于区分多个浮窗
                .setTag("testFloat")
                // 设置浮窗是否可拖拽
                .setDragEnable(true)
                // 设置浮窗固定坐标，ps：设置固定坐标，Gravity属性和offset属性将无效
                .setLocation(100, 200)
                // 设置浮窗的对齐方式和坐标偏移量
                .setGravity(Gravity.END | Gravity.CENTER_VERTICAL, 0, 200)
                // 设置宽高是否充满父布局，直接在xml设置match_parent属性无效
                .setMatchParent(false,false)
                // 设置Activity浮窗的出入动画，可自定义，实现相应接口即可（策略模式），无需动画直接设置为null
                .setAnimator(new DefaultAnimator())
                // 设置系统浮窗的出入动画，使用同上
                .registerCallbacks(new OnFloatCallbacks() {
                    @Override
                    public void createdResult(boolean b, String s, View view) {

                    }

                    @Override
                    public void show(View view) {

                    }

                    @Override
                    public void hide(View view) {

                    }

                    @Override
                    public void dismiss() {

                    }

                    @Override
                    public void touchEvent(View view, MotionEvent motionEvent) {

                    }

                    @Override
                    public void drag(View view, MotionEvent motionEvent) {

                    }

                    @Override
                    public void dragEnd(View view) {

                    }
                }).show();
    }
}
